#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 2e6 + 10;

int n, root, idx;
struct node
{
    int ls, rs, fa, size;
    char val;
}t[N];

void pushup(int x) { t[x].size = t[t[x].ls].size + t[t[x].rs].size + 1; }

char st[N];
int build(int l, int r, int f)
{
    if (l > r) return 0;
    int mid = (l + r) >> 1;
    int x = ++idx;
    t[x].fa = f;
    t[x].val = st[mid];
    t[x].ls = build(l, mid - 1, x);
    t[x].rs = build(mid + 1, r, x);
    pushup(x);
    return x;
}

bool get(int x) { return t[t[x].fa].rs == x; }

void rotate(int x)
{
    int f = t[x].fa, g = t[f].fa, son = get(x);
    if (son == 1) {
        t[f].rs = t[x].ls;
        if (t[f].rs != 0) t[t[f].rs].fa = f;
        t[x].ls = f;
    } else {
        t[f].ls = t[x].rs;
        if (t[f].ls != 0) t[t[f].ls].fa = f;
        t[x].rs = f;
    }
    t[f].fa = x;t[x].fa = g;
    if (g != 0) {
        if (t[g].rs == f) t[g].rs = x;
        else t[g].ls = x;
    }
    pushup(f);pushup(x);
}

void splay(int x, int goal)
{
    if (goal == 0) root = x;
    while (1) {
        int f = t[x].fa, g = t[f].fa;
        if (f == goal) break;
        if (goal != g) {
            if (get(x) == get(f)) rotate(f);
            else rotate(x);
        }
        rotate(x);
    }
    pushup(x);
}

int kth(int x, int k)
{
    if (t[t[x].ls].size + 1 == k) return x;
    if (t[t[x].ls].size >= k) return kth(t[x].ls, k);
    return kth(t[x].rs, k - t[t[x].ls].size - 1);
}

void insert(int l, int len)
{
    int x = kth(root, l), y = kth(root, l + 1);
    splay(x, 0);splay(y, x);
    t[y].ls = build(1, len, y);
    pushup(y);pushup(x);
}

void del(int l, int r)
{
    int x = kth(root, l), y = kth(root, r + 1);
    splay(x, 0); splay(y, x);
    t[y].ls = 0;
    pushup(y); pushup(x);
}

void dfs(int x)
{
    if (x == 0) return;
    dfs(t[x].ls);
    cout << t[x].val;
    dfs(t[x].rs);
}

int main()
{
    t[1].size = 2, t[1].ls = 2;
    t[2].size = 1, t[2].fa = 1;
    root = 1, idx = 2;
    cin >> n;
    char op[10], ch; int len, pos = 1;
    while (n--) {
        cin >> op;
        if (op[0] == 'I') {
            cin >> len;
            for (int i = 1;i <= len;i++) {
                char ch = getchar();
                while (ch < 32 || ch > 126)
                    ch = getchar();
                st[i] = ch;
            }
            insert(pos, len);
        }

        if (op[0] == 'G') {
            cin >> len;
            int x = kth(root, pos), y = kth(root, pos + len + 1);
            splay(x, 0);splay(y, x);
            dfs(t[y].ls); cout << '\n';
        }

        if (op[0] == 'D') { cin >> len; del(pos, pos + len); }
        if (op[0] == 'M') { cin >> pos; pos++; }
        if (op[0] == 'P') pos--;
        if (op[0] == 'N') pos++;
    }
}